
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Solution129 {

    int sum = 0;
    public int sumNumbers(TreeNode root) {
        if (root == null)
            return 0;
        preOrder(root, 0);
        return sum;
    }

    private void preOrder(TreeNode node, int num) {
        num = num * 10 + node.val;
        if (node.left == null && node.right == null) {
            sum += num;
            return;
        }

        if (node.left != null)
            preOrder(node, num);

        if (node.right != null)
            preOrder(node, num);
    }
}